package utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

// Druid tools.
public class Druid {

    // Log.
    protected final static Logger logger  = LoggerFactory.getLogger(Druid.class);
    // DataSource.
    private static DataSource ds;

    // Constructor.
    public Druid(Properties pro) {

        try {
            // Get DataSource.
            ds = DruidDataSourceFactory.createDataSource(pro);
            logger.info("创建 数据库连接池 成功");
        } catch (Exception e) {
            logger.error("创建 数据库连接池 错误 { " + e + " }");
        }

    }

    // Get Connection.
    public Connection getConnection() {

        try {
            Connection conn = ds.getConnection();
            logger.info("获取 数据库连接 <" + conn + "> 成功");
            return conn;
        } catch (SQLException throwables) {
            logger.error("从 连接池 <" + ds + ">中获取 数据库连接失败 { " + throwables + " }");
            return null;
        }

    }

    // Get Statement.
    public Statement getStmt(Connection conn) {

        try {
            Statement stmt = conn.createStatement();
            logger.info("获取 statement <" + stmt + "> 成功");
            return stmt;
        } catch (SQLException throwables) {
            logger.error("创建 statement 失败 { " + throwables + " }");
            return null;
        }

    }

    // Close Connection.
    public void close(Connection conn){
        if (conn!=null){
            try {
                conn.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        logger.info("释放 connection");
    }
    public void close(Statement stmt,Connection conn){
        if (stmt!=null) {
            try {
                stmt.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        logger.info("释放 statement、connection");
    }
    public void close(ResultSet rs, Statement stmt, Connection conn){
        if (rs!=null) {
            try {
                rs.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        if (stmt!=null) {
            try {
                stmt.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        logger.info("释放 resultset、 statement、connection");
    }

    // Get DataSource.
    public static DataSource getDataSource(){
        return ds;
    }

}
